package com.atguigu.lease.web.admin.schedule;

import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName ScheduledTasks
 * @Author admin
 * @Date 2024/6/3
 * created by jyl on 04/6/3
 */
@Component
@RequiredArgsConstructor
public class ScheduledTasks {

    private final LeaseAgreementService leaseAgreementService;

    //             [秒] [分] [时] [日] [月] [周] [年]
    @Scheduled(cron = "0 30 3 * * ?")
    public void checkLeaseStatus(){
        LambdaUpdateWrapper<LeaseAgreement> wrapper = new LambdaUpdateWrapper<>();
        // 当过期时间小于当前时间
        wrapper.le(LeaseAgreement::getLeaseEndDate,new Date());
        // 锁定状态范围 (只要[2(已签约),5(退租待确认)]),其他都不考虑
        wrapper.in(LeaseAgreement::getStatus, LeaseStatus.SIGNED,LeaseStatus.WITHDRAWING);
        // 执行任务具体操作 (检测到租约到期,更改为到期状态)
        wrapper.set(LeaseAgreement::getStatus,LeaseStatus.EXPIRED);

        leaseAgreementService.update(wrapper);

    }


}
